package com.glsc.ngateway.common.api.common.utils;

import org.apache.logging.log4j.util.Strings;

import javax.validation.constraints.NotBlank;

/**
 * 自定义字符串工具类
 *
 * @author xuchang
 */
public class GatewayStrUtil {
    /**
     * 驼峰转小写下划线：personId -> person_id
     */
    public static String camelCase2SnakeCase(@NotBlank String str) {
        StringBuilder sb = new StringBuilder();
        // 前置字符是否大写
        boolean preCharIsUpperCase = true;
        // 当前字符是否大写
        boolean curreCharIsUpperCase = true;
        // 下一字符是否大写
        boolean nexteCharIsUpperCase = true;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (i > 0) {
                preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
            } else {
                preCharIsUpperCase = false;
            }
            curreCharIsUpperCase = Character.isUpperCase(c);
            if (i < (str.length() - 1)) {
                nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
            }
            if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) {
                sb.append("_");
            } else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase) {
                sb.append("_");
            }
            sb.append(Character.toLowerCase(c));
        }

        return sb.toString();
    }

    /**
     * 小写下划线转驼峰：person_id -> personId
     */
    public static String snakeCase2CamelCase(@NotBlank String s) {
        //全小写
        s = s.toLowerCase();
        StringBuilder sb = new StringBuilder(s.length());
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '_') {
                upperCase = true;
            } else if (upperCase) {
                sb.append(Character.toUpperCase(c));
                upperCase = false;
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 编号比较器（编号格式：XXX-XXX...）
     *
     * @return 正数: str1 > str2
     * @return 0:   str1 = str2
     * @return 负数: str1 < str2
     */
    public static int sequenceCompare(String str1, String str2) {
        if (Strings.isBlank(str1)) {
            return -1;
        } else if (Strings.isBlank(str2)) {
            return 1;
        } else {
            String[] split1 = str1.split("-");
            String[] split2 = str2.split("-");
            int i = 0, len1 = split1.length, len2 = split2.length;
            for (; i < len1 && i < len2; i++) {
                String s1 = split1[i];
                String s2 = split2[i];
                //如果相等，继续比较下一位
                if (s1.equals(s2)) {
                    continue;
                } else {
                    //检查是否有字母
                    boolean hasDigit1 = false, hasDigit2 = false;
                    for (char s : s1.toCharArray()) {
                        if (!Character.isDigit(s)) {
                            hasDigit1 = true;
                            break;
                        }
                    }
                    for (char s : s2.toCharArray()) {
                        if (!Character.isDigit(s)) {
                            hasDigit2 = true;
                            break;
                        }
                    }
                    //只有其中一个有，有字母的大
                    if (hasDigit1 != hasDigit2) {
                        return hasDigit1 ? 1 : -1;
                    }
                    //都是数字，直接比较
                    if (!hasDigit1) {
                        return Integer.parseInt(s1) - Integer.parseInt(s2);
                    } else {
                        //都是字母
                        //长度不同，较长的大
                        if (s1.length() != s2.length()) {
                            return s1.length() > s2.length() ? 1 : -1;
                        } else {
                            //长度相同，比较其字典序
                            return s1.compareTo(s2);
                        }
                    }
                }
            }
            //都比较完了，若两者长度相等，则相等，否则较长的大
            return len1 == len2 ? 0 : len1 - len2;
        }
    }
}
